package com.astrob.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.astrob.model.TurnItem;
import com.astrob.nhcmap.R;

import java.util.List;

public class TurnAdapter extends BaseAdapter {
	private final String TAG = "TurnAdapter";
	private Context context = null;
	private List<TurnItem> dataList = null;

	public TurnAdapter(Context context) {
		this.context = context;
	}

	public void setDataList(List<TurnItem> dataList) {
		this.dataList = dataList;
	}

	@Override
	public int getCount() {
		if (dataList == null || dataList.isEmpty()) {
			return 0;
		}
		return dataList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return dataList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (null == convertView) {
			convertView = LinearLayout.inflate(context,
					R.layout.adapter_routeinfolist, null);

			holder = new ViewHolder();

			holder.turnName = (TextView) convertView
					.findViewById(R.id.turn_item_name);

			holder.turnDis = (TextView) convertView
					.findViewById(R.id.turn_item_dis);
			holder.turnType = (ImageView) convertView
					.findViewById(R.id.turn_item_type);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final TurnItem item = (TurnItem) getItem(position);
		holder.turnName.setText(item.getName());
		if (item.getDistanceStr().compareToIgnoreCase("change") == 0) {
			holder.turnDis.setText("");
			holder.turnName.setTextColor(context.getResources().getColor(R.color.red));
		} else {
			holder.turnDis.setText(item.getDistanceStr());	
			holder.turnName.setTextColor(context.getResources().getColor(R.color.listview_text_color));
		}
		
		holder.turnType.setBackgroundResource(item.getType());

		return convertView;
	}

	static class ViewHolder {
		TextView turnName;
		TextView turnDis;
		ImageView turnType;
	}

}
